﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HNG.LIB.Controls;
using HNG.Portal.BLL;
using HNG.Portal.DAL;
using HNG.Portal.DAL.EntityClasses;
using HNG.Portal.DAL.FactoryClasses;
using HNG.Portal.DAL.HelperClasses;
using SD.LLBLGen.Pro.ORMSupportClasses;
using System.Web.UI.HtmlControls;
using System.Data;
using System.IO;
public partial class AdminPortal_Controls_Banner_Slide_Info : System.Web.UI.UserControl
{
    TblBannerManager _TblBannerManager = new TblBannerManager();
    public int BID = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try { BID = Convert.ToInt32(Request.QueryString["id"].Trim()); }
            catch { BID = 0; }
            if (BID == 0) //Add cate
            {
                lblTitle.Text = "Thêm slide";
                Toolbox1.ShowDel = false;
                Toolbox1.ShowRefesh = Toolbox1.ShowBack = false;
                imgbtn_Delete.Enabled = false;
                img_news.Visible = imgbtn_Delete.Visible = false;
            }
            else //sua cate
            {
                imgbtn_Delete.Enabled = true;
                lblDelete.Visible = true;
                imgbtn_Delete.ImageUrl = "~/AdminPortal/images/icon/remove_16.gif";
                lblTitle.Text = "Sửa slide: " + BID;
                BindData(BID);
            }
        }
    }

    //load chi tiet san pham
    protected void BindData(int ID)
    {
        TblBannerEntity _TblBannerEntity = _TblBannerManager.SelectOne(ID);
        if (_TblBannerEntity == null) { lblthongbao.Text = "<img src='../../Images/icon/Icon_warning.gif' alt='Cảnh báo' /> <span style='font-size:11px;color:red;'>  Không có danh mục này </span>"; return; }
        txtID.Text = ID.ToString();
        hidID.Value = ID.ToString();
        txtMoTa.Text = _TblBannerEntity.MoTa;
        txtTenbanner.Text = Server.HtmlDecode(_TblBannerEntity.TenBanner);
        txtUrl.Text = Server.HtmlDecode(_TblBannerEntity.Url);
        txtWidth.Text = _TblBannerEntity.Dai.ToString();
        txtHeight.Text = _TblBannerEntity.Rong.ToString();
        txtDateTo.Text = Convert.ToDateTime(_TblBannerEntity.NgayBatDau).ToString("MM/dd/yyyy");
        txtDateFrom.Text = Convert.ToDateTime(_TblBannerEntity.NgayKetThuc).ToString("MM/dd/yyyy");
        img_news.Visible = true;
        img_news.ImageUrl = (_TblBannerEntity.Image != "") ? Server.HtmlDecode(_TblBannerEntity.Image) : "~/AdminPortal/Images/icon/no_image.jpg";
        if (string.IsNullOrEmpty(_TblBannerEntity.Image))
        {
            imgbtn_Delete.Enabled = false;
            imgbtn_Delete.ImageUrl = "~/AdminPortal/images/icon/remove_16_hiden.gif";
            img_news.Visible = false;
        }
        else
        {
            img_news.Visible = true;
        }
        chkStatus.Checked = Convert.ToBoolean(_TblBannerEntity.TrangThai);
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        BindToData();
        Response.Redirect(SiteMap.CurrentNode.ParentNode.Url);
    }
    protected void btnDel_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(hidID.Value))
        {
            _TblBannerManager.Delete(Convert.ToInt32(hidID.Value));
        }
        Response.Redirect(SiteMap.CurrentNode.ParentNode.Url);
    }
    protected void btnApply_Click(object sender, EventArgs e)
    {
        if (txtTenbanner.Text == "") { lblthongbao.Text = "<img src='../Images/icon/Icon_warning.gif' alt='Cảnh báo' />  <span style='font-size:11px;color:red;'> Nhập tên banner !</span>"; txtTenbanner.Focus(); return; }
        BindToData();
        Response.Redirect(ResolveUrl("~/AdminPortal/Pages/SlideEdit.aspx?id=") + hidID.Value);
        //BindData(Convert.ToInt32(hidID.Value));
    }

    private void BindToData()
    {
        TblBannerEntity _TblBannerEntity = new TblBannerEntity();
        if (!string.IsNullOrEmpty(hidID.Value))
            _TblBannerEntity.Id = Convert.ToInt32(hidID.Value);

        _TblBannerEntity.TenBanner = Server.HtmlEncode(txtTenbanner.Text);
        _TblBannerEntity.Url = Server.HtmlEncode(txtUrl.Text);
        _TblBannerEntity.Type = "slide";
        _TblBannerEntity.MoTa = txtMoTa.Text;
        if (!string.IsNullOrEmpty(txtHeight.Text))
            _TblBannerEntity.Dai = Convert.ToInt32(txtWidth.Text);
        if (!string.IsNullOrEmpty(txtWidth.Text))
            _TblBannerEntity.Rong = Convert.ToInt32(txtHeight.Text);
        if (!string.IsNullOrEmpty(txtDateTo.Text))
            _TblBannerEntity.NgayBatDau = Convert.ToDateTime(txtDateTo.Text);
        if (!string.IsNullOrEmpty(txtDateFrom.Text))
            _TblBannerEntity.NgayKetThuc = Convert.ToDateTime(txtDateFrom.Text);
        _TblBannerEntity.LangId = HNG.LIB.Localization.LocalizationUtils.GetCurrentLanguage();
        _TblBannerEntity.NgayTao = DateTime.Now;
        _TblBannerEntity.TrangThai = chkStatus.Checked;
        if (!string.IsNullOrEmpty(txtThuTu.Text))
            _TblBannerEntity.ThuTu = Convert.ToInt32(txtThuTu.Text);
        if (FileUpload1.FilePath != "")
        {
            string ImageDefault = Server.MapPath(_TblBannerEntity.Image);
            if (File.Exists(ImageDefault)) File.Delete(ImageDefault);
            _TblBannerEntity.Image = Server.HtmlEncode(FileUpload1.FilePath);
        }
        if (!string.IsNullOrEmpty(hidID.Value))
        {
            _TblBannerManager.Update(_TblBannerEntity);
        }
        else
        {
            _TblBannerManager.Insert(_TblBannerEntity);
            hidID.Value = _TblBannerEntity.Id.ToString();
        }
    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect(SiteMap.CurrentNode.ParentNode.Url);
    }
    protected void imgbtn_Delete_Click(object sender, ImageClickEventArgs e)
    {
        if (!string.IsNullOrEmpty(hidID.Value))
        {
            TblBannerEntity _TblBannerEntity = _TblBannerManager.SelectOne(Convert.ToInt32(hidID.Value));
            if (_TblBannerEntity != null)
            {
                if (!string.IsNullOrEmpty(_TblBannerEntity.Image))
                {
                    string defaultImage = Server.MapPath(_TblBannerEntity.Image);
                    try { File.Delete(defaultImage); }
                    catch { }
                    _TblBannerEntity.Image = "";
                    _TblBannerManager.Update(_TblBannerEntity);
                }
            }
        }
        BindData(Convert.ToInt32(hidID.Value));
    }
}